from flask import Flask, request, redirect
import tools, json, conf

app = Flask(__name__)


@app.route('/code')
def code():
    resp = conf.Resp.creatresp(conf.Resp)
    (resp.response, resp.headers['set-cookie']) = tools.code(request.headers.get('cookie'))

    return resp


@app.route('/login', methods=['POST'])
def login():
    resp = conf.Resp.creatresp(conf.Resp)
    try:
        un = request.form['username']
        pwd = request.form['password']
        code = request.form['captcha']
        resp.response, resp.status_code = tools.login(un, pwd, code, request.headers.get('cookie'))
    except:
        resp.response, resp.status_code = json.dumps({"msg": "请正确填写"}), 403

    return resp


@app.route('/emailCode', methods=['POST'])
def emailCode():
    resp = conf.Resp.creatresp(conf.Resp)
    try:
        email = request.form['email']
        try:
            changepwd = request.form['changepwd']
        except:
            changepwd = None
        resp.response=tools.emailcode(email, changepwd)
    except:
        resp.response = json.dumps({"msg": "邮箱不能为空"})

    return resp


@app.route('/register', methods=['POST'])
def register():
    resp = conf.Resp.creatresp(conf.Resp)
    try:
        email = request.form['email']
        password = request.form['password']
        phone = request.form['phone']
        verificationCode = request.form['verificationCode']
        try:
            invitationCode = request.form['invitationCode']
        except:
            invitationCode = None
        resp.response = tools.register(email, password, phone, verificationCode, invitationCode)
    except:
        resp.response = json.dumps({"msg": "参数填写不规范"})

    return resp


@app.route('/changepwd', methods=['POST'])
def changepwd():
    resp = conf.Resp.creatresp(conf.Resp)
    try:
        email = request.form['email']
        password = request.form['password']
        verificationCode = request.form['verificationCode']
        resp.response = tools.changepwd(email, password, verificationCode)
    except:
        resp.response = json.dumps({"msg": "参数填写不规范"})

    return resp


@app.route('/import', methods=['POST', 'OPTIONS'])
def Improt():
    resp = conf.Resp.creatresp(conf.Resp)
    resp.response = 'success'
    if request.method == 'POST':
        username = conf.Redis.createcon(conf.Redis, 2).get(request.headers.get('myCookie'))
        if username == None:
            resp.response = "请先登录"
        else:
            try:
                username = json.loads(username.decode('utf8'))['username']
                url = request.form['url']
                un = request.form['un']
                if '://' in url:
                    url = url.split('/')
                    url = url[0] + '//' + url[2]
                else:
                    url = 'https://' + url.split('/')[0]
                resp.response = tools.Import(url, un, username)
            except:
                resp.response = "参数填写不全或不规范"

    return resp


@app.route('/home', methods=['POST', 'OPTIONS'])
def homelist():
    resp = conf.Resp.creatresp(conf.Resp)
    resp.response = 'success'
    if request.method == 'POST':
        username = conf.Redis.createcon(conf.Redis, 2).get(request.headers.get('myCookie'))
        if username == None:
            resp.response = "请先登录"
        else:
            username = json.loads(username.decode('utf8'))['username']
            resp.response = json.dumps(tools.homelist(username))

    return resp


@app.route('/update', methods=['POST', 'OPTIONS'])
def update():
    resp = conf.Resp.creatresp(conf.Resp)
    resp.response = 'success'
    if request.method == 'POST':
        username = conf.Redis.createcon(conf.Redis, 2).get(request.headers.get('myCookie'))
        if username == None:
            resp.response = "请先登录"
        else:
            username = json.loads(username.decode('utf8'))['username']
            tools.update(username)
            resp.response = '正在更新'

    return resp


@app.route('/addurl', methods=['POST', 'OPTIONS'])
def addurl():
    resp = conf.Resp.creatresp(conf.Resp)
    resp.response = 'success'
    if request.method == 'POST':
        username = conf.Redis.createcon(conf.Redis, 2).get(request.headers.get('myCookie'))
        if username == None:
            resp.response = "请先登录"
        else:
            try:
                username = json.loads(username.decode('utf8'))['username']
                url = request.form['url']
                un = request.form['un']
                pwd = request.form['pwd']
                if '://' in url:
                    url = url.split('/')
                    url = url[0] + '//' + url[2]
                else:
                    url = 'https://' + url.split('/')[0]
                resp.response = tools.addUrl(url, un, pwd, username)
            except:
                resp.response = "参数填写不全或不规范"

    return resp


@app.route('/del', methods=['POST', 'OPTIONS'])
def delun():
    resp = conf.Resp.creatresp(conf.Resp)
    resp.response = 'success'
    if request.method == 'POST':
        username = conf.Redis.createcon(conf.Redis, 2).get(request.headers.get('myCookie'))
        if username == None:
            resp.response = "请先登录"
        else:
            username = json.loads(username.decode('utf8'))['username']
            try:
                un = request.form['un']
                resp.response = tools.delun(un, username)
            except:
                resp.response = "参数填写不全或不规范"

    return resp


@app.route('/add', methods=['POST', 'OPTIONS'])
def add():
    resp = conf.Resp.creatresp(conf.Resp)
    resp.response = 'success'
    if request.method == 'POST':
        username = conf.Redis.createcon(conf.Redis, 2).get(request.headers.get('myCookie'))
        if username == None:
            resp.response = "请先登录"
        else:
            username = json.loads(username.decode('utf8'))['username']
            try:
                url = request.form['url']
                id = request.form['id']
                un = request.form['un']
                pwd = request.form['pwd']
                resp.response = tools.add(url, id, un, pwd, username)
            except:
                resp.response = "无法获取计划详情"

    return resp


@app.route('/updatepwd', methods=['POST', 'OPTIONS'])
def updatepwd():
    resp = conf.Resp.creatresp(conf.Resp)
    resp.response = 'success'
    if request.method == 'POST':
        username = conf.Redis.createcon(conf.Redis, 2).get(request.headers.get('myCookie'))
        if username == None:
            resp.response = "请先登录"
        else:
            username = json.loads(username.decode('utf8'))['username']
            try:
                url = request.form['url']
                un = request.form['un']
                pwd = request.form['pwd']
                resp.response = tools.updatepwd(username, url, un, pwd)
            except:
                resp.response = "参数填写不全或不规范"

    return resp


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=conf.Other.webport)
